package com.chenx.myapplicationnav.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.room.Room.databaseBuilder
import com.chenx.myapplicationnav.databinding.FragmentHomeBinding
import com.chenx.myapplicationnav.ui.home.db.AppDatabase
import com.chenx.myapplicationnav.ui.home.db.User


class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

//        val textView: TextView = binding.textHome
//        homeViewModel.text.observe(viewLifecycleOwner) {
//            textView.text = it
//        }
//
//        homeViewModel.textlist.observe(viewLifecycleOwner){
//            textView.text = it.get(0)
//        }

        //test
//        var mytext= MutableLiveData<String>().apply {
//            value = "1221323This is home Fragment"
//        }
//
//        homeViewModel.text=mytext

        val recyclerView: RecyclerView = binding.recyclerView
        recyclerView.layoutManager = LinearLayoutManager(context)

        homeViewModel.images.observe(viewLifecycleOwner, { imageUrls ->
            val adapter = ImageAdapter(imageUrls)
            recyclerView.adapter = adapter
        })
        // homeViewModel.fetchImages("https://www.qq.com")


        var webSite = ChinaZ()
        webSite.parse(homeViewModel.viewModelScope, homeViewModel._images);


        //db

        // 获取数据库实例
        // 获取数据库实例
//        val db = databaseBuilder(
//            requireContext(),
//            AppDatabase::class.java, "user.db"
//        ).build()


        var userDao = AppDatabase.getInstance(requireContext())?.userDao();
        // 使用数据库
        //  val userDao = db.userDao()
        // 插入数据
        try {
            val user = User()
            user.uid = 110
            user.firstName = "John"
            user.lastName = "Doe"
            userDao!!.insertAll(user)
            user.uid = 112
            user.firstName = "John"
            user.lastName = "Doe"
            userDao!!.insertAll(user)
            user.uid = 113
            user.firstName = "John"
            user.lastName = "Doe"
            userDao!!.insertAll(user)
        } catch (e: Exception) {
        }


        // 查询数据
        val users = userDao!!.all


        // 创建动态对象
//        val dynamicObject: MutableMap<String, Any> = HashMap()
//        dynamicObject["Name"] = "John"
//        dynamicObject["Age"] = 30
//
//
//        val group: MutableList<Map<String, Any>> = ArrayList()
//
//        // 添加第一个动态对象
//        val dynamicObject1: MutableMap<String, Any> = HashMap()
//        dynamicObject1["Name"] = "John"
//        dynamicObject1["Age"] = 30
//        group.add(dynamicObject1)
//
//        // 添加第二个动态对象
//        val dynamicObject2: MutableMap<String, Any> = HashMap()
//        dynamicObject2["Name"] = "Alice"
//        dynamicObject2["Age"] = 25
//        group.add(dynamicObject2)
//
//        // 使用Gson库将列表对象转换为JSON字符串
//        val gson = Gson()
//        val jsonString: String = gson.toJson(group)
//
//        // 使用增强型 for 循环遍历组中的每个动态对象
//        for (dynamicObject in group) {
//            // 遍历动态对象中的键值对
//            dynamicObject.get("Age")
//            for ((key, value) in dynamicObject) {
//                println("Key: $key, Value: $value")
//            }
//        }


        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}